VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "XbrlDoc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'Copyright [2012] [Fundamental X]

'   Licensed under the Apache License, Version 2.0 (the "License");
'   you may not use this file except in compliance with the License.
'   You may obtain a copy of the License at

'       http://www.apache.org/licenses/LICENSE-2.0

'   Unless required by applicable law or agreed to in writing, software
'   distributed under the License is distributed on an "AS IS" BASIS,
'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'   See the License for the specific language governing permissions and
'   limitations under the License.
Public filing As XbrlFiling
Dim objXML As Object
Public f_name As String
Public ticker As String
Public aNo As String
Dim endDateDate As Date
Dim periodsToXLS() As Obj3
Dim period1i As Integer
Dim period2i As Integer
Dim ls() As Obj1
Dim preRels() As XbrlPreRel
Dim refItems() As XbrlRefEle
Dim refTxtItems() As XbrlRefEle
Dim items() As XbrlElement
Property Get itemTot() As Integer

itemTot = UBound(items)

End Property
Sub initRefItems()

Dim i As Integer

ReDim refItems(16)
For i = 1 To 11
    Set refItems(i) = New XbrlRefEle
Next i

refItems(1).load 1, "dei", "entityregistrantname"
refItems(2).load 2, "xxl", "entitytradingsymbol"
refItems(3).load 3, "dei", "documenttype"
refItems(4).load 4, "dei", "amendmentflag"
refItems(5).load 5, "dei", "amendmentdescription"
refItems(6).load 6, "dei", "entitywellknownseasonedissuer"
refItems(7).load 7, "dei", "entityfilercategory"
refItems(8).load 8, "dei", "testTxt"
refItems(9).load 9, "xxl", "documentfilingdate"
refItems(10).load 10, "dei", "documentperiodenddate"
refItems(11).load 11, "dei", "currentfiscalyearenddate"

For i = 12 To 16
    Set refItems(i) = New XbrlRefEle
Next i

refItems(12).load 12, "dei", "documentfiscalyearfocus"
refItems(13).load 13, "dei", "entitycentralindexkey"
refItems(14).load 14, "dei", "entitypublicfloat"
refItems(15).load 15, "dei", "entitycommonstocksharesoutstanding"
refItems(16).load 16, "dei", "testNum"

End Sub
Sub postRefProcessing()

refItems(10).value = CDate(refItems(10).value)
refItems(11).value = CDate(Mid$(refItems(11).value, 3))

End Sub
Sub addFiling(f As ADODB.Recordset)

Set filing = New XbrlFiling
filing.loadFromDB f
f_name = Xbrl.f_outRoot & filing.accessionNo & "\" & filing.ticker & "-" & filing.endDate
endDateDate = Xbrl.convLongDate(filing.endDate)

End Sub
Sub initDOM()

Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.Async = False
objXML.setProperty "ServerHTTPRequest", True
objXML.ResolveExternals = True
objXML.ValidateOnParse = True
objXML.load (f_name & prefix)

End Sub
Function getPeriodToXLS(n As Integer) As Obj3

Set getPeriodToXLS = periodsToXLS(n)

End Function
Function getRefItems() As XbrlRefEle()

getRefItems = refItems

End Function
Function getItems() As XbrlElement()

getItems = items

End Function
Function getPreRels() As XbrlPreRel()

getPreRels = preRels

End Function
Function getSections() As Obj1()

getSections = sections

End Function
Sub trace2()

Dim crhvs() As Obj3
Dim vrqws() As Obj3
Dim contextNodes As Object, contextNode As Object
Dim Obj3Node As Object
Dim lNode1 As Object, lNode2 As Object
Dim currID As String
Dim perTot As Integer, durTot As Integer
Dim dupEndlBegi As Integer, dupEndlEndi As Integer
Dim currcrhvi As Integer, bestDuri As Integer
Dim srchObj3 As Obj3
Dim Obj32 As Obj3, Obj33 As Obj3

Dim i As Integer


objXML.load (f_name & ".xml")
objXML.setProperty "SelectionLanguage", "XPath"

If (objXML.parseError.ErrorCode = 0) Then
    Set contextNodes = objXML.DocumentElement.SelectNodes("//*[local-name()='context']")
    ReDim crhvs(contextNodes.length)
    ReDim vrqws(contextNodes.length)
    For Each contextNode In contextNodes
        currID = contextNode.Attributes.GetNamedItem("id").NodeValue
        If contextNode.FirstChild.ChildNodes.length = 1 Then
            Set Obj3Node = contextNode.ChildNodes.Item(1)
            Set lNode1 = Obj3Node.ChildNodes.Item(0)
            If lNode1.BaseName = "instant" Then
                perTot = perTot + 1
                Set crhvs(perTot) = New Obj3
                crhvs(perTot).endl = lNode1.Text
                crhvs(perTot).id = currID
            Else
                durTot = durTot + 1
                Set vrqws(durTot) = New Obj3
                vrqws(durTot).endl = Obj3Node.ChildNodes.Item(1).Text
                vrqws(durTot).begl = lNode1.Text
                vrqws(durTot).id = currID
            End If
        End If
    Next
End If

For i = 1 To perTot
    teststr = crhvs(i).id
    testda = crhvs(i).endl
Next i

For i = 1 To durTot
    teststr = vrqws(i).id
Next i

ReDim Obj3sToXLS(6)

ReDim Preserve crhvs(perTot)
Utilities.insertSort crhvs, 1, perTot, 5
currcrhvi = Utilities.binSearchObj(5, Me, crhvs, True)
Set Obj3sToXLS(1) = crhvs(currcrhvi)
ReDim Preserve vrqws(durTot)
Utilities.insertSort vrqws, 1, durTot, 5
dupEndlBegi = Utilities.binSearchObj(5, Obj3sToXLS(1), vrqws, True)
dupEndlEndi = Utilities.binSearchObj(5, Obj3sToXLS(1), vrqws, False)
If dupEndlBegi = dupEndlEndi Then
    bestDuri = dupEndlBegi
Else
    Set srchObj3 = New Obj3
    srchObj3.g = 400
    Utilities.insertSort vrqws, dupEndlBegi, dupEndlEndi, 11
    bestDuri = Utilities.binAreaSrchObj(11, srchObj3, vrqws, True, -1, dupEndlBegi, dupEndlEndi)
    If vrqws(bestDuri).g < 175 Then
        bestDuri = dupEndlEndi
    End If
End If

Set Obj3sToXLS(2) = vrqws(bestDuri)
testd = Obj3sToXLS(2).endl
testdy = Obj3sToXLS(2).g
    

Set Obj32 = New Obj3
Obj32.endl = lAdd("d", -1, Obj3sToXLS(2).begl)
currcrhvi = Utilities.binSearchObj(5, Obj32, crhvs, True)
Set Obj3sToXLS(3) = crhvs(currcrhvi)
Utilities.insertSort vrqws, 1, durTot, 5
dupEndlBegi = Utilities.binSearchObj(5, Obj3sToXLS(3), vrqws, True)
dupEndlEndi = Utilities.binSearchObj(5, Obj3sToXLS(3), vrqws, False)
If dupEndlBegi = dupEndlEndi Then
    bestDuri = dupEndlBegi
Else
    Set srchObj3 = New Obj3
    srchObj3.g = 400
    Utilities.insertSort vrqws, dupEndlBegi, dupEndlEndi, 11
    bestDuri = Utilities.binAreaSrchObj(11, srchObj3, vrqws, True, -1, dupEndlBegi, dupEndlEndi)
    If vrqws(bestDuri).g < 175 Then
        bestDuri = dupEndlEndi
    End If
End If
Set Obj3sToXLS(4) = vrqws(bestDuri)


Set Obj33 = New Obj3
Obj33.endl = lAdd("d", -1, Obj3sToXLS(4).begl) 'Takes a day off.
currcrhvi = Utilities.binSearchObj(5, Obj33, crhvs, True)
Set Obj3sToXLS(5) = crhvs(currcrhvi)
Utilities.insertSort vrqws, 1, durTot, 5
dupEndlBegi = Utilities.binSearchObj(5, Obj33, vrqws, True)
dupEndlEndi = Utilities.binSearchObj(5, Obj33, vrqws, False)
If dupEndlBegi = dupEndlEndi Then
    bestDuri = dupEndlBegi
    Set srchObj3 = New Obj3
    srchObj3.g = 400
    Utilities.insertSort vrqws, dupEndlBegi, dupEndlEndi, 11
    bestDuri = Utilities.binAreaSrchObj(11, srchObj3, vrqws, True, -1, dupEndlBegi, dupEndlEndi)
    If vrqws(bestDuri).g < 175 Then
        bestDuri = dupEndlEndi
    End If
End If
Set Obj3sToXLS(6) = vrqws(bestDuri)



teste1 = Obj3sToXLS(1).endl
testb1 = Obj3sToXLS(2).begl
testd1 = Obj3sToXLS(2).g
teste2 = Obj3sToXLS(3).endl
testb2 = Obj3sToXLS(4).begl
testd2 = Obj3sToXLS(4).g
teste3 = Obj3sToXLS(6).endl
testb3 = Obj3sToXLS(6).begl


End Sub

Sub ttrw4()

Dim sectionNodes As Object, sectionNode As Object
Dim currSection As Obj1
Dim currSectionURI As String
Dim desc As String
Dim sectionInfo() As String
Dim sectionTot As Integer
Dim sNode As Object


objXML.load (f_name & ".xsd")

If (objXML.parseError.ErrorCode = 0) Then
    Set sectionNodes = objXML.DocumentElement.SelectNodes("//link:roleType")
    ReDim sections(sectionNodes.length)
    For Each sectionNode In sectionNodes
        Set currSection = New Obj1
        currSection.init
       currSectionURI = sectionNode.Attributes.GetNamedItem("roleURI").NodeValue
        currSection.stdName = LCase(Mid$(currSectionURI, InStrRev(currSectionURI, "/") + 1))
        desc = sectionNode.FirstChild.Text
        sectionInfo = Split(desc, "-", 3)
        currSection.preOrder = sectionInfo(0)
        currSection.repName = LTrim(sectionInfo(UBound(sectionInfo)))
        If UBound(sectionInfo) > 0 Then
            If StrComp(Left$(LTrim(sectionInfo(1)), 4), "Stat") = 0 Then
                currSection.category = 1
            Else
                currSection.category = 3
            End If
        End If
        sectionTot = sectionTot + 1
        Set sections(sectionTot) = currSection
    Next
End If



End Sub
Sub ssqu7()

Dim statementNodes As Object, statementNode As Object
Dim topNode As Object, preNodes As Object, preNode As Object
Dim currlURI As String, currlName As String
Dim lTot As Integer, preRelSectBeg As Integer, preRelTot As Integer
Dim currli As Integer
Dim currl As Obj1, currPreRel As XbrlPreRel
Dim currPreRels() As XbrlPreRel, currAlphaPreRels() As XbrlPreRel
Dim currFromTag As String
Dim toMatchi As Integer
Dim currRef As String
Dim i As Integer

Utilities.quickSortTxtObj ls, 1, UBound(ls), 3

objXML.load (f_name & "_pre.xml")
objXML.setProperty "SelectionLanguage", "XPath"

If (objXML.parseError.ErrorCode = 0) Then
    Set statementNodes = objXML.DocumentElement.SelectNodes("//*[local-name()='presentationLink']")
    testl = statementNodes.length
    For Each statementNode In statementNodes
        preRelTot = 0
        currlURI = statementNode.Attributes.GetNamedItem("xlink:role").NodeValue
        currlName = LCase(Mid$(currlURI, InStrRev(currlURI, "/") + 1))
        currli = Utilities.binSearchTxtObj(3, currlName, ls)
        Set currl = ls(currli)
        Set preNodes = statementNode.SelectNodes("*[local-name()='presentationArc']")
        ReDim currPreRels(0 To preNodes.length) As XbrlPreRel
        For Each preNode In preNodes
            Set currPreRel = New XbrlPreRel
            Set currPreRel.l = currl
            currRef = preNode.Attributes.GetNamedItem("xlink:to").NodeValue
            currPreRel.tag = LCase(Mid$(currRef, InStr(currRef, "_") + 1))
            currRef = preNode.Attributes.GetNamedItem("xlink:from").NodeValue
            currPreRel.fromTag = LCase(Mid$(currRef, InStr(currRef, "_") + 1))
            currPreRel.eleOrder = preNode.Attributes.GetNamedItem("order").NodeValue
            preRelTot = preRelTot + 1
            Set currPreRels(preRelTot) = currPreRel
        Next
        If preRelTot > 0 Then
            ReDim currAlphaPreRels(1 To preRelTot) As XbrlPreRel
            For i = 1 To preRelTot
                Set currAlphaPreRels(i) = currPreRels(i)
            Next i
            Utilities.quickSortTxtObj currAlphaPreRels, 1, preRelTot, 3
            currFromTag = currPreRels(1).fromTag
            Do
                toMatchi = Utilities.binSearchTxtObj(3, currFromTag, currAlphaPreRels)
                If toMatchi > 0 Then
                    currFromTag = currAlphaPreRels(toMatchi).fromTag
                Else
                    Set currPreRels(0) = New XbrlPreRel
                    currPreRels(0).tag = currFromTag
                    Exit Do
                End If
            Loop
            currl.estOrder currPreRels
        End If
    Next
End If


End Sub
Sub bbgggt5()

Dim elements() As XbrlEleHldr
Dim eleNodes As Object, eleNode As Object
Dim eleXPath As String
Dim elementTot As Integer, itemTot As Integer
Dim matchIndex As Integer
Dim tmpSwap As Object
Dim i As Integer, j As Integer

Utilities.insertSortTxtObj refItems, 1, 16, 5

objXML.load (f_name & ".xml")
objXML.setProperty "SelectionLanguage", "XPath"

If (objXML.parseError.ErrorCode = 0) Then
    ReDim elements(objXML.DocumentElement.ChildNodes.length)
    For i = 1 To 6
        eleXPath = "*[@contextRef='" & periodsToXLS(i).id & "'][not(substring(name(), string-length(name())- 4)='Block')]"  'contains(name(), 'Block')
        Set eleNodes = objXML.DocumentElement.SelectNodes(eleXPath)
        For Each eleNode In eleNodes
            testv = eleNode.BaseName
            testp = eleNode.prefix
            If StrComp(eleNode.prefix, "dei") = 0 Then
                matchIndex = Utilities.binSearchTxtObj(5, LCase(eleNode.BaseName), refItems)
                If matchIndex > 0 Then
                    refItems(matchIndex).value = eleNode.Text
                End If
            Else
                elementTot = elementTot + 1
                Set elements(elementTot) = New XbrlEleHldr
                Set elements(elementTot).element = eleNode
                elements(elementTot).name = LCase(eleNode.BaseName)
                elements(elementTot).yr = i - Int(i / 2)
                elements(elementTot).isDuri = -1 * periodsToXLS(i).days
            End If
        Next
    Next i
    
End If

Utilities.insertSort refItems, 1, 16, 2

ReDim Preserve elements(elementTot)
Utilities.quickSort elements, 1, elementTot, 3



ReDim items(0 To elementTot)
Set items(0) = New XbrlElement
For i = 1 To elementTot
    currSchema = elements(i).element.prefix
    If StrComp(elements(i).name, items(itemTot).tag) > 0 Then
        itemTot = itemTot + 1
        Set items(itemTot) = New XbrlElement
        items(itemTot).tag = elements(i).name
        items(itemTot).schema = currSchema
        On Error Resume Next
        items(itemTot).dec = elements(i).element.Attributes.GetNamedItem("decimals").NodeValue
        On Error GoTo 0
        items(itemTot).isDuri = elements(i).isDuri
        items(itemTot).value(elements(i).yr) = elements(i).element.Text
    Else
        items(itemTot).value(elements(i).yr) = elements(i).element.Text
    End If
Next i


ReDim Preserve items(itemTot)

Set objXML = Nothing

End Sub
Sub extractItems()

Dim fs As Object
Dim srcFile As Object

Dim currLine As String
Dim currEndCh As Integer, currBegCh As Integer
Dim currTag As String
Dim currAttrib As String
Dim startPos As Integer
Dim currPerID As String
Dim decimals As String
Dim currcy As String
Dim currValue As Single
Dim perMatchi As Integer, tagMatchi As Integer

Dim i As Integer

ReDim allItems(1000) As XbrlItem
Utilities.insertSortTxtObj periodsToXLS, 1, 4, 15
For i = 1 To 4
    periodsToXLS(i).initItems
Next i

Set fs = CreateObject("Scripting.FileSystemObject")
Set srcFile = fs.OpenTextFile(f_name & ".xml", 1)

Do While Not srcFile.AtEndofStream
    currLine = LTrim(srcFile.readLine)
    If Mid$(currLine, 2, 4) = "us-g" Then
        currTag = Utilities.parseStr(currLine, ":", " ", 9)
        startPos = Len(currTag) + 10
        currAttrib = Utilities.parseStr(currLine, " ", "=", startPos)
        Do
            Select Case Left(currAttrib, 1)
                Case "c"
                    startPos = startPos + 12
                    currPerID = LCase(Utilities.parseStr(currLine, """", """", startPos))
                    startPos = startPos + Len(currPerID) + 2
                Case "d"
                    startPos = startPos + 10
                    decimals = LCase(Utilities.parseStr(currLine, """", """", startPos))
                    startPos = startPos + Len(decimals) + 2
                Case "u"
                    startPos = startPos + 9
                    currcy = LCase(Utilities.parseStr(currLine, """", """", startPos))
                    startPos = startPos + Len(currcy) + 2
                Case Else
                     startPos = startPos + Len(currAttrib) + 4 '2 for the tokens and 2 for the quotes around the value
            End Select
            currAttrib = Utilities.parseStr(currLine, " ", "=", startPos)
        Loop While Len(currAttrib) > 0
        perMatchi = Utilities.binSearchTxtObj(15, currPerID, periodsToXLS)
        If perMatchi > 0 Then
            tagMatchi = Utilities.binSearchTxtObj(3, LCase(currTag), Xbrl.items)
            If tagMatchi > 0 Then
                 currValStr = Utilities.parseStr(currLine, ">", "<", startPos)
                If Len(currValStr) > 0 Then
                    currValue = currValStr
                    periodsToXLS(perMatchi).addItem tagMatchi, currValue
                End If
            End If
        End If
    End If
Loop

srcFile.Close

For i = 1 To 4
    periodsToXLS(i).shrinkItems
Next i

End Sub
Sub uuflii1()

Dim currPreRels() As XbrlPreRel
Dim currItemNo As Integer
Dim i As Integer, j As Integer

Utilities.quickSort sections, 1, UBound(sections), 2

For i = UBound(sections) To 1 Step -1
    tests = sections(i).stdName
    currPreRels = sections(i).preRels
    For j = UBound(currPreRels) To 1 Step -1
        currItemNo = Utilities.binSearchTxtObj(3, currPreRels(j).tag, items)
        If currItemNo > 0 Then
            currPreRels(j).itemNo = currItemNo
           currPreRels(j).prevSection = items(currItemNo).section
            currPreRels(j).prevPreRel = items(currItemNo).preRel
             items(currItemNo).section = i
            items(currItemNo).preRel = j
        End If
    Next j
Next i

End Sub
Property Get compkey(keyNo As Integer) As Variant

Select Case keyNo
    Case 0
    Case 5
        compkey = endDateDate
End Select

End Property



